<?php
/**
 * Description: 腾讯AI 美妆&年龄获取
 * Author: hezhizheng
 * Date: 2018/5/11
 * Time: 9:31
 * Created by Created by Panxsoft.
 */

namespace App\Services\TenCentAI;

//require_once app_path('Libs/TenCentAI/SDK/API.php');
//require_once app_path('Libs/TenCentAI/SDK/Configer.php');
//require_once app_path('Libs/TenCentAI/SDK/HttpUtil.php');
//require_once app_path('Libs/TenCentAI/SDK/Signature.php');
require_once app_path('Libs/TenCentAI/include.php');

class TenCentAIService
{

    private function setAIAppInfo()
    {
        $app_id = config('pkx.tenCent_ai.app_id');
        $appkey = config('pkx.tenCent_ai.app_key');
        return \Configer::setAppInfo($app_id, $appkey);
    }


    /**
     * 美妆
     * @test
     * @see TenCentAIServiceTest::test_ptuFaceCosmetic()
     * @param string $img_url
     * @param int $id
     * @return array|mixed
     */
    public function ptuFaceCosmetic(string $img_url, int $id): array
    {
        $this->setAIAppInfo();

        $data = file_get_contents($img_url);
        $base64 = base64_encode($data);

        // 设置请求数据
        $params = array(
            'app_id'     => config('pkx.tenCent_ai.app_id'),
            'image'      => $base64,
            'cosmetic'   => $id,
            'time_stamp' => strval(time()),
            'nonce_str'  => strval(rand()),
            'sign'       => '',
        );

        $params['sign'] = \Signature::getReqSign($params);

        // 执行API调用
        $url = 'https://api.ai.qq.com/fcgi-bin/ptu/ptu_facecosmetic';

        $response = \HttpUtil::doHttpPost($url, $params);

        return json_decode($response, true);

    }

    /**
     * 年龄
     * @test
     * @see TenCentAIServiceTest::test_ptuFaceAge()
     * @param string $img_url
     * @return mixed
     */
    public function ptuFaceAge(string $img_url): array
    {
        $this->setAIAppInfo();

        $data = file_get_contents($img_url);
        $base64 = base64_encode($data);

        $params = array(
            'app_id'     => config('pkx.tenCent_ai.app_id'),
            'image'      => $base64,
            'time_stamp' => strval(time()),
            'nonce_str'  => strval(rand()),
            'sign'       => '',
        );

        $params['sign'] = \Signature::getReqSign($params);

        $url = 'https://api.ai.qq.com/fcgi-bin/ptu/ptu_faceage';
        $response = \HttpUtil::doHttpPost($url, $params);

        return json_decode($response, true);
    }
}